package lanou.giftsay_01.fragment.categorybody;


import android.content.Intent;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Button;
import android.widget.GridLayout;
import android.widget.Switch;
import android.widget.TextView;

import lanou.giftsay_01.adapter.FenggeRvAdapter;
import lanou.giftsay_01.adapter.GonglueRvAdapter;
import lanou.giftsay_01.R;
import lanou.giftsay_01.adapter.NormalRvAdapter;
import lanou.giftsay_01.bean.CategoryBean;
import lanou.giftsay_01.bean.NormalBean;
import lanou.giftsay_01.main.categorymain.DuixiangActivity;
import lanou.giftsay_01.main.categorymain.FenggeActivity;
import lanou.giftsay_01.main.categorymain.GonglueBodyActivity;
import lanou.giftsay_01.main.categorymain.LanmuActivity;
import lanou.giftsay_01.main.categorymain.NormalActitivty;
import lanou.giftsay_01.tools.BaseFragment;
import lanou.giftsay_01.tools.NetTool;
import lanou.giftsay_01.tools.URLValues;


/**
 * Created by dllo on 16/8/18.
 */
public class GonglueFragment extends BaseFragment implements View.OnClickListener {

    private RecyclerView gouLueRv ,pinLeiRv , fengGeRv , duiXiangRv;
    private GonglueRvAdapter gonglueRvAdapter  ;
    private NormalRvAdapter normalRvAdapter1   ;
    private TextView lanMu;
    private TextView pinLei;
    private TextView fengGe;
    private TextView duiXiang;
    private Intent intent;
    private Intent intent1;
    private Intent intent2;

    @Override
    protected int setLayout() {
        return R.layout.fragment_gonglue;
    }

    @Override
    protected void initView() {
        gouLueRv = (RecyclerView) getView().findViewById(R.id.goulue_rv);
        pinLeiRv = (RecyclerView) getView().findViewById(R.id.pinlei_rv);
        fengGeRv = (RecyclerView) getView().findViewById(R.id.fengge_rv);
        duiXiangRv = (RecyclerView) getView().findViewById(R.id.duixiang_rv);
        lanMu = (TextView) getView().findViewById(R.id.tv_sreach_lanmu);
        pinLei = (TextView) getView().findViewById(R.id.tv_sreach_pinlei);
        fengGe = (TextView) getView().findViewById(R.id.tv_sreach_fengge);
        duiXiang = (TextView) getView().findViewById(R.id.tv_sreach_duixiang);
        lanMu.setOnClickListener(this);
        pinLei.setOnClickListener(this);
        fengGe.setOnClickListener(this);
        duiXiang.setOnClickListener(this);
        GridLayoutManager manager = new GridLayoutManager(mContext ,3);
        manager.setOrientation(GridLayoutManager.HORIZONTAL);
        gonglueRvAdapter = new GonglueRvAdapter(mContext);

        gouLueRv.setLayoutManager(manager);

    }

    @Override
    protected void initData() {

        NetTool netTool = new NetTool();
        netTool.getNetData(URLValues.SPECIALCATEGORY, CategoryBean.class, new NetTool.NetListener<CategoryBean>() {
            @Override
            public void onSuccess(CategoryBean categoryBean) {
                gonglueRvAdapter.setCategoryBean(categoryBean);
                gouLueRv.setAdapter(gonglueRvAdapter);
                /**
                 * 栏目的接口回调
                 */
               gonglueRvAdapter.setOnClickCateGoryItem(new OnClickCateGoryItem() {
                    @Override
                    public void onClick(int position, CategoryBean categoryBean) {
                        if(position<11){
                            Intent intent = new Intent(GonglueFragment.this.getContext(),GonglueBodyActivity.class);
                            intent.putExtra("id" , String.valueOf(categoryBean.getData().getColumns().get(position).getId()));
                            startActivity(intent);
                        }else {
                            Intent intent = new Intent(GonglueFragment.this.getContext(),LanmuActivity.class);
                            startActivity(intent);
                        }
                    }
                });


            }

            @Override
            public void onError(String errorMsg) {

            }
        });
        gouLueRv.setAdapter(gonglueRvAdapter);
        netTool.getNetData(URLValues.WANGGE, NormalBean.class, new NetTool.NetListener<NormalBean>() {
            @Override
            public void onSuccess(NormalBean normalBean) {
                NormalRvAdapter adapter0 = new NormalRvAdapter(getContext(), 0);
                GridLayoutManager manager0 = new GridLayoutManager(getContext(),2);
                adapter0.setNormalBean(normalBean);
                pinLeiRv.setLayoutManager(manager0);
                pinLeiRv.setAdapter(adapter0);
                adapter0.setNormalItemOnClickListener(new NormalItemOnClickListener() {
                    @Override
                    public void onClick(int position, NormalBean normalBean, int tag) {
                        Intent intent = new Intent(GonglueFragment.this.getContext(),NormalActitivty.class);
                        intent.putExtra("id" , String.valueOf(normalBean.getData().getChannel_groups().get(tag).getChannels().get(position).getId()));
                        intent.putExtra("name" ,normalBean.getData().getChannel_groups().get(tag).getChannels().get(position).getName());
                        startActivity(intent);
                    }
                });

                NormalRvAdapter adapter1 = new NormalRvAdapter(getContext(), 1);
                GridLayoutManager manager1 = new GridLayoutManager(getContext(),2);
                adapter1.setNormalBean(normalBean);
                fengGeRv.setLayoutManager(manager1);
                fengGeRv.setAdapter(adapter1);
                adapter1.setNormalItemOnClickListener(new NormalItemOnClickListener() {
                    @Override
                    public void onClick(int position, NormalBean normalBean, int tag) {
                        Intent intent = new Intent(GonglueFragment.this.getContext(),NormalActitivty.class);
                        intent.putExtra("id" , String.valueOf(normalBean.getData().getChannel_groups().get(tag).getChannels().get(position).getId()));
                        intent.putExtra("name" ,normalBean.getData().getChannel_groups().get(tag).getChannels().get(position).getName());
                        startActivity(intent);
                    }
                });

                NormalRvAdapter adapter2 = new NormalRvAdapter(getContext(), 2);
                GridLayoutManager manager2 = new GridLayoutManager(getContext(),2);
                adapter2.setNormalBean(normalBean);
                adapter2.setNormalItemOnClickListener(new NormalItemOnClickListener() {
                    @Override
                    public void onClick(int position, NormalBean normalBean, int tag) {
                        Intent intent = new Intent(GonglueFragment.this.getContext(),NormalActitivty.class);
                        intent.putExtra("id" , String.valueOf(normalBean.getData().getChannel_groups().get(tag).getChannels().get(position).getId()));
                        intent.putExtra("name" ,normalBean.getData().getChannel_groups().get(tag).getChannels().get(position).getName());
                        startActivity(intent);
                    }
                });
                duiXiangRv.setLayoutManager(manager2);
                duiXiangRv.setAdapter(adapter2);

            }

            @Override
            public void onError(String errorMsg) {

            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_sreach_lanmu:
                intent = new Intent(GonglueFragment.this.getContext() , LanmuActivity.class);
                startActivity(intent);
                break;
            case R.id.tv_sreach_pinlei:
                intent1 = new Intent(GonglueFragment.this.getContext(),FenggeActivity.class);
                startActivity(intent1);
                break;
            case R.id.tv_sreach_fengge:

                break;
            case R.id.tv_sreach_duixiang:
                intent2 = new Intent(GonglueFragment.this.getContext() ,DuixiangActivity.class);
                startActivity(intent2);
                break;
        }

    }
}
